package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.CaregiverInfoMapper;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.dao.EmployeeMapper;
import com.tjetc.dao.PhyExamMapper;
import com.tjetc.entity.*;
import com.tjetc.service.PhyExamService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
@Service
public class PhyExamServiceImpl implements PhyExamService {
    @Autowired
    private PhyExamMapper phyExamMapper;
    @Autowired
    private ElderInfoMapper elderInfoMapper;
    @Autowired
    private CaregiverInfoMapper caregiverInfoMapper;
    @Override
    public JsonResult findById(Long id) {
        PhyExam phyExam=phyExamMapper.selectById(id);
        ElderInfo elderInfo=elderInfoMapper.selectById(phyExam.getElderlyId());
        CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(phyExam.getCaregiverId());
        phyExam.setElderInfo(elderInfo);
        phyExam.setCaregiverInfo(caregiverInfo);
        return JsonResult.success(phyExam);
    }
    @Override
    public   JsonResult findElderPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<PhyExam> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<PhyExam>phyExamPage= phyExamMapper.selectPageLikeElder(page,name);
        for (PhyExam phyExam : phyExamPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(phyExam.getElderlyId());
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(phyExam.getCaregiverId());
            phyExam.setElderInfo(elderInfo);
            phyExam.setCaregiverInfo(caregiverInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(phyExamPage);
    }
    @Override
    public   JsonResult findCaregiverPage(String name, Integer pageNo, Integer pageSize){
        //设置页码和每页数量
        Page<PhyExam> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<PhyExam>phyExamPage= phyExamMapper.selectPageLikeCaregiver(page,name);
        for (PhyExam phyExam : phyExamPage.getRecords()) {
            ElderInfo elderInfo=elderInfoMapper.selectById(phyExam.getElderlyId());
            CaregiverInfo caregiverInfo=caregiverInfoMapper.selectById(phyExam.getCaregiverId());
            phyExam.setElderInfo(elderInfo);
            phyExam.setCaregiverInfo(caregiverInfo);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(phyExamPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=phyExamMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(PhyExam phyExamParam) {
        PhyExam phyExam=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(phyExamParam.getCheckId()==null||phyExamParam.getCheckId()<=0){
            //新增
            phyExam=new PhyExam();
            BeanUtils.copyProperties(phyExamParam,phyExam);
            phyExam.setCreateTime(now);
            phyExam.setUpdateTime(now);
            phyExamMapper.insert(phyExam);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            phyExam=phyExamMapper.selectById(phyExamParam.getCheckId());
            if(phyExam==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            phyExam.setUpdateTime(now);
            phyExam.setCaregiverInfo(phyExamParam.getCaregiverInfo());
            phyExam.setElderInfo(phyExamParam.getElderInfo());
            phyExam.setBloodSugar(phyExamParam.getBloodSugar());
            phyExam.setCaregiverId(phyExamParam.getCaregiverId());
            phyExam.setCheckResult(phyExamParam.getCheckResult());
            phyExam.setCheckTime(phyExamParam.getCheckTime());
            phyExam.setDiastolicPressure(phyExamParam.getDiastolicPressure());
            phyExam.setElderlyId(phyExamParam.getElderlyId());
            phyExam.setHealthStatus(phyExamParam.getHealthStatus());
            phyExam.setHeartRate(phyExamParam.getHeartRate());
            phyExam.setHeight(phyExamParam.getHeight());
            phyExam.setIsDeleted(phyExamParam.getIsDeleted());
            phyExam.setSuggestion(phyExamParam.getSuggestion());
            phyExam.setSystolicPressure(phyExamParam.getSystolicPressure());
            phyExam.setTemperature(phyExamParam.getTemperature());
            phyExam.setWeight(phyExamParam.getWeight());
            int rows=phyExamMapper.updateById(phyExam);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
}
